package com.android.dopewars;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

public class Dealer {
	public Dealer(String name, boolean gender_is_male) {
		name_ = name;
		gender_is_male_ = gender_is_male;
	}
	public Dealer(String from_string) {
		valueFrom(from_string);
	}
	public String name() { return name_; }
	public void setName(String new_name) {
		name_ = new_name;
	}
	public void setFoundHustlerMode(boolean new_found_hustler_mode) {
		found_hustler_mode_ = new_found_hustler_mode;
	}
	public boolean foundHustlerMode() {
		return found_hustler_mode_;
	}
	public boolean gender_is_male() { return gender_is_male_; }
	public void setGenderIsMale(boolean gender_is_male) {
		gender_is_male_ = gender_is_male;
	}
	public int cash() { return cash_; }
	public void setCash(int new_cash) {
		cash_ = new_cash;
		if (cash_ > highest_cash_amount_) {
			highest_cash_amount_ = cash_;
		}
	}
	public int highest_cash_amount() { return highest_cash_amount_;	}
	public void reset_highest_cash_amount() { highest_cash_amount_ = 0; }
	
	public int bank() { return bank_; }
	public void setBank(int new_bank) {
		bank_ = new_bank;
	}
	public int debt() { return debt_; }
	public void setDebt(int new_debt) {
		debt_ = new_debt;
	}
	public int total_space() { return total_space_; }
	public void setTotalSpace(int new_total_space) {
		total_space_ = new_total_space;
	}
	public int spaceAvailable() {
		int space_used = 0;
    	Iterator<String> inv_it = drug_inventory_.keySet().iterator();
    	while (inv_it.hasNext()) {
    		String next = inv_it.next();
    		space_used += drug_inventory_.get(next);
    	}
    	return total_space_ - space_used;
	}
	public HashMap<String, Integer> debug_inventory() { return drug_inventory_; }
	public int health() {
		return health_;
	}
	public void setHealth(int new_health) {
		health_ = new_health;
	}
	public void addGun(Gun new_gun) {
		guns_.add(new_gun);
	}
	public int numGuns() {
		return guns_.size();
	}
	public Gun gun(int i) {
		return guns_.elementAt(i);
	}
	public int firepower() {
		int firepower = 0;
		for (int i = 0; i < guns_.size(); ++i) {
			firepower += guns_.elementAt(i).low_damage();
		}
		return firepower;
	}
	public void removeGun(int i) {
		guns_.removeElementAt(i);
	}
	public int drugQuantity(String drug_name) {
		Integer ret_val = drug_inventory_.get(drug_name);
		if (ret_val == null) {
			return 0;
		}
		return ret_val;
	}
	public void addDrugsToInventory(String name, int quantity) {
		int prev_value = 0;
		if (drug_inventory_.get(name) != null) {
			prev_value = drug_inventory_.get(name);
		}
		drug_inventory_.put(name, prev_value + quantity);
	}
	public void removeDrugsFromInventory(String name, int quantity) {
		int prev_value = 0;
		if (drug_inventory_.get(name) != null) {
			prev_value = drug_inventory_.get(name);
		}
		drug_inventory_.put(name, prev_value - quantity);
	}
	public boolean hasDrug(String name) {
		return (drug_inventory_.get(name) != null);
	}
	public void removeAllInventory() {
		drug_inventory_.clear();
		guns_.clear();
	}
	
	public void addGameScore(int game_mode_, int game_score_) {
		// Assume that game scores are sorted from high to low.
		int insert_point = 0;
		Vector<Integer> game_mode_scores = game_scores_.get(game_mode_);
		if (game_mode_scores == null) {
			game_mode_scores = new Vector<Integer>();
			game_scores_.put(game_mode_, game_mode_scores);
		}
		while (game_mode_scores.size() > insert_point) {
			if (game_mode_scores.elementAt(insert_point) < game_score_) {
				break;
			}
			++insert_point;
		}
		if (insert_point == game_mode_scores.size()) {
			if (insert_point < 100) {
				game_mode_scores.add(game_score_);
			}
		} else {
			game_mode_scores.insertElementAt(game_score_, insert_point);
		}
	}
	public Vector<Integer> gameScores(int game_mode_) {
		return game_scores_.get(game_mode_);
	}
	
	@Override
	public String toString() {
		String ret_string = new String();
		ret_string += "{";
		name_ = name_.replace("(", "").replace(")", "").replace("{", "").replace("}", "").replace(",","");
		ret_string += "(Name," + name_ + ")";
		ret_string += "(Male," + Boolean.toString(gender_is_male_) + ")";
		ret_string += "(Cash," + Integer.toString(cash_) + ")";
		ret_string += "(Debt," + Integer.toString(debt_) + ")";
		ret_string += "(Bank," + Integer.toString(bank_) + ")";
		ret_string += "(TotalSpace," + Integer.toString(total_space_) + ")";
		ret_string += "(Health," + Integer.toString(health_) + ")";
		for (int i = 0; i < guns_.size(); ++i) {
			ret_string += "(Gun," + guns_.elementAt(i).toString() + ")";
		}
		Iterator<String> inv_it = drug_inventory_.keySet().iterator();
    	while (inv_it.hasNext()) {
    		String next = inv_it.next();
    		ret_string += "(Cookie," + next + "," + Integer.toString(drug_inventory_.get(next)) + ")";
    	}
    	Set<Integer> game_modes = game_scores_.keySet();
    	Iterator<Integer> modes_it = game_modes.iterator();
    	while (modes_it.hasNext()) {
    		int next_mode = modes_it.next();
    		ret_string += "(GameScores," + Integer.toString(next_mode);
    		for (int i = 0; i < game_scores_.get(next_mode).size(); ++i) {
    			ret_string += "," + Integer.toString(game_scores_.get(next_mode).elementAt(i));
    		}
    		ret_string += ")";
    	}
    	ret_string += "}";
    	
		return ret_string;
	}
	
	public void processVar(String key, String value) {
		if (key.equals("Name")) {
			name_ = value;
		} else if (key.equals("Male")) {
			gender_is_male_ = Boolean.parseBoolean(value);
		} else if (key.equals("Cash")) {
			cash_ = Integer.parseInt(value);
		} else if (key.equals("Debt")) {
			debt_ = Integer.parseInt(value);
		} else if (key.equals("Bank")) {
			bank_ = Integer.parseInt(value);
		} else if (key.equals("TotalSpace")) {
			total_space_ = Integer.parseInt(value);
		} else if (key.equals("Health")) {
			health_ = Integer.parseInt(value);
		} else if (key.equals("Gun")) {
			guns_.add(new Gun(value));
		} else if (key.equals("Drug")) {
			String[] pieces = value.split(",");
			if (pieces.length == 2) {
				drug_inventory_.put(pieces[0], Integer.parseInt(pieces[1]));
			}
		} else if (key.equals("GameScores")) {
			String[] pieces = value.split(",");
			int next_mode = Integer.parseInt(pieces[0]);
			game_scores_.put(next_mode, new Vector<Integer>());
			for (int i = 1; i < pieces.length; ++i) {
				game_scores_.get(next_mode).add(Integer.parseInt(pieces[i]));
			}
		}
	}
	
	public void valueFrom(String s) {
		if (s.charAt(0) == '{') {
			String tmp_string = s.substring(1);
			while ((tmp_string.indexOf("(") >= 0) && (tmp_string.indexOf(")") > 0)) {
				String next_var = tmp_string.substring(tmp_string.indexOf("(") + 1,tmp_string.indexOf(")"));
				tmp_string = tmp_string.substring(tmp_string.indexOf(")") + 1);
				if (next_var.indexOf(",") >= 0) {
					String next_key = next_var.substring(0, next_var.indexOf(","));
					String next_value = next_var.substring(next_var.indexOf(",") + 1);
					processVar(next_key, next_value);
				}
			}
		} else {
			String[] pieces = s.split(",");
			assert pieces.length >= 10;
			name_ = pieces[0];
			gender_is_male_ = Boolean.valueOf(pieces[1]);
			cash_ = Integer.valueOf(pieces[2]);
			debt_ = Integer.valueOf(pieces[3]);
			bank_ = Integer.valueOf(pieces[4]);
			total_space_ = Integer.valueOf(pieces[5]);
			health_ = Integer.valueOf(pieces[6]);
			guns_.setSize(Integer.valueOf(pieces[7]));
			for (int i = 0; i < guns_.size(); ++i) {
				guns_.setElementAt((new Gun(pieces[8 + i])), i);
			}
			int num_drugs = Integer.valueOf(pieces[8 + guns_.size()]);
			for (int i = 0; i < num_drugs; i += 2) {
				drug_inventory_.put(pieces[9 + guns_.size() + i], Integer.valueOf(pieces[10 + guns_.size() + i]));
			}
			/* these scores are dead to me now
			game_scores_.setSize(Integer.valueOf(pieces[9 + guns_.size() + 2 * num_drugs]));
			for (int i = 0; i < game_scores_.size(); ++i) {
				game_scores_.setElementAt(Integer.valueOf(pieces[9 + guns_.size() + 2 * num_drugs + i + 1]), i);
			}
			*/
		}
	}
	
	String name_;
	boolean gender_is_male_;
	int cash_;
	int highest_cash_amount_;
	int debt_;
	int bank_;
	int total_space_;
	int health_;
	boolean found_hustler_mode_ = false;
	Vector<Gun> guns_ = new Vector<Gun>();
	HashMap<String, Integer> drug_inventory_ = new HashMap<String, Integer>();
	
	HashMap<Integer, Vector<Integer>> game_scores_ = new HashMap<Integer, Vector<Integer>>();
}
